package com.huarui.company.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.PagingInfo;

import com.huarui.company.model.Company;
import com.huarui.company.service.CompanyService;
import com.huarui.companydeal.model.CompanyDeal;
import com.huarui.companydeal.service.CompanyDealService;
import com.huarui.saleReturn.util.ConstantParamEnum;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
import com.huarui.util.ConstantUtil;

public class CompanyManageAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -717117815693611512L;
	private static final Logger logger = Logger.getLogger(CompanyManageAction.class);
	private String queryParam;
	private Map<String, Object> jsonObject;
	// 分页
	private Integer start;
	private Integer limit;
	private String sort;
	private String dir;
	
	private CompanyService companyService;
	private CompanyDealService companyDealService;
	@SuppressWarnings("unchecked")
	public String queryCompany(){
		jsonObject = new HashMap();
		if (queryParam != null) {
			JSONObject queryObject = JSONObject.fromObject(queryParam);
			jsonObject = (Map) JSONObject.toBean(queryObject, Map.class);
		}
		Integer count = companyService.getCompanyCountByConditions(jsonObject);
		jsonObject = queryMapParamUtil(jsonObject);
		List<Company> companys = companyService.getAllCompanyByConditions(jsonObject);		
		jsonObject.clear();
		jsonObject.put(ConstantParamEnum.TOTALPROPERTY.getType(), count);
		jsonObject.put(ConstantParamEnum.ROOT.getType(), companys);
		return SUCCESS;
	}

	private Map<String,Object> queryMapParamUtil(Map<String,Object> jsonObject) {		
		jsonObject.put("start",start ==null ? 0: start);
		jsonObject.put("pageSize",limit == null ? 20: limit);
		jsonObject.put("orderBy", sort == null ? "gmtCreate":sort);
		
		if (dir != null){
			jsonObject.put("desc", dir.equals(ConstantUtil.DESC) ? true : false);			
		}else{
			jsonObject.put("desc",true);	
			
		}		
		return jsonObject;
	}
	/**
	 * 查询公司账户余额
	 * @return
	 */
	public String queryBalance(){
		SessionMessage sessionMessage = UserHolder.getCurrentSessionMessage();
		jsonObject = new HashMap<String, Object>();
		try {
			Company company = (Company) companyService.getById(sessionMessage.getCompanyId());
			jsonObject.put("balance", company.getBalance());
			jsonObject.put("success", true);
		} catch (Exception e) {
			logger.error("查询账户余额失败.......", e);
			jsonObject.put("success", false);
			jsonObject.put("msg", "查询账户余额失败.......");
		}
		return SUCCESS;
		
	}
	/**
	 * 赠送货币
	 * @return
	 */
	public String presentPackage(){
		jsonObject = new HashMap<String, Object>();
		try {
			companyService.addCompanyBalance(queryParam,httpServletRequest.getRemoteAddr());
			jsonObject.put("success", true);
			jsonObject.put("msg", "赠送货币成功!");
		} catch (Exception e) {
			logger.error("赠送货币失败.......", e);
			jsonObject.put("success", false);
			jsonObject.put("msg", "赠送货币失败.......");
		}
		return SUCCESS;
		
	}
	
	@SuppressWarnings("unchecked")
	public String queryCompanyDeal(){
		jsonObject = new HashMap();
		if (queryParam != null) {
			JSONObject queryObject = JSONObject.fromObject(queryParam);
			jsonObject = (Map) JSONObject.toBean(queryObject, Map.class);
		}
		PagingInfo<Map<String,Object>, CompanyDeal> pagingInfo = new PagingInfo<Map<String,Object>, CompanyDeal>();
		pagingInfo.setCondition(jsonObject);
		pagingInfo.setStart(start ==null ? 0: start);
		pagingInfo.setLimit(limit == null ? 20: limit);
		companyDealService.getCompanyDealsByCondition(pagingInfo);
		ServletActionContext.getContext().put("jsonResult", pagingInfo);
		return SUCCESS;
	}
	
	public String getQueryParam() {
		return queryParam;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}	

	public void setStart(Integer start) {
		this.start = start;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public Map<String, Object> getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(Map<String, Object> jsonObject) {
		this.jsonObject = jsonObject;
	}

	public void setCompanyDealService(CompanyDealService companyDealService) {
		this.companyDealService = companyDealService;
	}
}
